package com.leon.android.component.explore

import com.billy.cc.core.component.CC
import com.billy.cc.core.component.CCResult
import com.billy.cc.core.component.IComponent
import com.leon.android.common.action.IActionProcessor
import java.util.concurrent.atomic.AtomicBoolean


/**
 * @time:2019/12/16 10:44
 * @author:Leon
 * @description:
 */
class ComponentExplore:IComponent {
//    override fun onCall(cc: CC): Boolean {
//        when(cc.actionName){
//            "fragment" -> {
//                CC.sendCCResult(cc.callId, CCResult.success("fragment",ExploreFragment()))
//            }
//            else -> {
//                CC.sendCCResult(cc.callId, CCResult.error("服务未找到"))
//            }
//        }
//        return false
//    }


    private val initialized: AtomicBoolean = AtomicBoolean(false)
    private val map: HashMap<String, IActionProcessor> = HashMap()

    private fun initProcessors() { //cc-regsiter插件会自动生成注册代码在此处，如：
//add(new ShowLoginActivityProcessor())
    }

    private fun add(processor: IExploreActionProcessor) {
        map[processor.actionName] = processor
    }

    override fun onCall(cc: CC): Boolean {
        if (initialized.compareAndSet(false, true)) {
            synchronized(map) { initProcessors() }
        }
        val actionName = cc.actionName
        val processor = map[actionName]
        if (processor != null) {
            return processor.onActionCall(cc)
        }
        CC.sendCCResult(cc.callId, CCResult.errorUnsupportedActionName())
        return false
    }

    override fun getName(): String = "explore"
}